package com.zrwl.poscloud.xo.service.impl;

import com.zrwl.poscloud.base.entity.TimeTableDetail;
import com.zrwl.poscloud.base.entity.UserEmployeeR;
import com.zrwl.poscloud.base.entity.UserServiceConfigDetail;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrwl.poscloud.base.enums.EStatus;
import com.zrwl.poscloud.base.superI.SuperServiceImpl;
import com.zrwl.poscloud.commons.utils.ResultUtil;
import com.zrwl.poscloud.commons.utils.StringUtils;
import com.zrwl.poscloud.xo.global.MessageConf;
import com.zrwl.poscloud.xo.global.SQLConf;
import com.zrwl.poscloud.xo.global.SysConf;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.zrwl.poscloud.xo.mapper.UserServiceConfigDetailMapper;
import com.zrwl.poscloud.xo.service.UserServiceConfigDetailService;
import com.zrwl.poscloud.xo.vo.UserServiceConfigDetailVO;

import javax.annotation.Resource;

/**
 * 商品内容类型 服务实现类
 *
 * @author 刘思童 AutoGenerate
 * @date 2023-05-24
 */
@Service
public class UserServiceConfigDetailServiceImpl extends SuperServiceImpl<UserServiceConfigDetailMapper, UserServiceConfigDetail> implements UserServiceConfigDetailService {

    @Autowired
    private UserServiceConfigDetailService userServiceConfigDetailService;
    @Resource
    private UserServiceConfigDetailMapper userServiceConfigDetailMapper;

    @Override
    public IPage<UserServiceConfigDetail> getPageList(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        QueryWrapper<UserServiceConfigDetail> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(userServiceConfigDetailVO.getKeyword()) && !StringUtils.isEmpty(userServiceConfigDetailVO.getKeyword().trim())) {
            queryWrapper.like("name", userServiceConfigDetailVO.getKeyword().trim());
        }
        Page<UserServiceConfigDetail> page = new Page<>();
        page.setCurrent(userServiceConfigDetailVO.getCurrentPage());
        page.setSize(userServiceConfigDetailVO.getPageSize());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByDesc(SQLConf.CREATE_TIME);
        IPage<UserServiceConfigDetail> pageList = userServiceConfigDetailService.page(page, queryWrapper);
        List<UserServiceConfigDetail> list = pageList.getRecords();
        pageList.setRecords(list);
        return pageList;
    }

    @Override
    public List<UserServiceConfigDetail> getAllList(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        QueryWrapper<UserServiceConfigDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_uid", userServiceConfigDetailVO.getBatchUid());
        queryWrapper.ne(SQLConf.STATUS, EStatus.DISABLED);
        queryWrapper.orderByAsc(SQLConf.CREATE_TIME);
        List<UserServiceConfigDetail> list = userServiceConfigDetailService.list(queryWrapper);
        if (list.size()>0){
            return list;
        }else {
            return null;
        }
    }

    @Override
    public String add(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        //判重
        QueryWrapper<UserServiceConfigDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_uid", userServiceConfigDetailVO.getBatchUid());
        queryWrapper.eq("pro_type", userServiceConfigDetailVO.getProType());
        queryWrapper.ne("status", EStatus.DISABLED);
        List<UserServiceConfigDetail> list = userServiceConfigDetailService.list(queryWrapper);
        if (list.size()>0){
            return ResultUtil.errorWithMessage("该商品内容已经存在");
        }else {
            UserServiceConfigDetail entity=new UserServiceConfigDetail();
            BeanUtils.copyProperties(userServiceConfigDetailVO, entity, SysConf.STATUS);
            entity.insert();
            userServiceConfigDetailVO.setUid(entity.getUid());
        }
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String edit(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        //判重
        QueryWrapper<UserServiceConfigDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("batch_uid", userServiceConfigDetailVO.getBatchUid());
        queryWrapper.eq("pro_type", userServiceConfigDetailVO.getProType());
        queryWrapper.ne("status", EStatus.DISABLED);
        List<UserServiceConfigDetail> list = userServiceConfigDetailService.list(queryWrapper);
        if (list.size()>0){
            return ResultUtil.errorWithMessage("该商品内容已经存在");
        }else {
            UserServiceConfigDetail entity = userServiceConfigDetailService.getById(userServiceConfigDetailVO.getUid());
            BeanUtils.copyProperties(userServiceConfigDetailVO, entity);
            entity.updateById();
            return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
        }
    }

    @Override
    public String delete(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        UserServiceConfigDetail entity = userServiceConfigDetailService.getById(userServiceConfigDetailVO.getUid());
        entity.setStatus(EStatus.DISABLED);
        entity.updateById();
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public List<UserServiceConfigDetailVO> getServiceConfigDetailInfo(UserServiceConfigDetailVO userServiceConfigDetailVO) {
        return userServiceConfigDetailMapper.getServiceConfigDetailInfo(userServiceConfigDetailVO);
    }
}
